package zju.dgm.sqlcfg;


import MiniSQL.JDBCUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * @author lzy
 */
@Component
public class JdbcHandle extends JDBCUtils {

    @Value("${server.port}")
    private Integer serverPort;


    @PostConstruct
    void init() throws IOException {
        File f = new File(System.getProperty("user.dir") + "/dbpath-" + serverPort + "/");
        if(f.exists()){
            for(File sub: Objects.requireNonNull(f.listFiles())){
                sub.delete();
            }
            assert f.delete();
        }
        connect("root", "root", "dbpath-" + serverPort);
    }

    @PreDestroy
    void destroy() throws IOException {
        disconnect();
    }
}
